﻿using com.nm.ApiServer;
using com.nm.ApiServer.Attributes;
using com.nm.Attributes;
using com.nm.db.Models.Terminal;
using george.Data;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Nancy.Extensions;
using TerminalAPI.models;
using george.Helper;

namespace TerminalAPI
{
    [Description("店铺陈列SKC")]
    public class ShopSKCHandler : BaseHandler
    {
        [MustLogin]
        [Description("店铺陈列SKC明细列表")]
        [ReturnType(typeof(PaperData<ShelfDispalyModel.ReportSKCModel>))]
        public ApiResult GetList(
           [ParamDescription("搜索关键字")] string Keys = "",
           [ParamDescription("陈列类别")] string Category = "",
           [ParamDescription("年份")] string Year = "",
           [ParamDescription("季节")] string Season = "",
           [ParamDescription("页码")]int Page = 1,
           [ParamDescription("页数")]int Size = 10)
        {
            var dc = new TerminalEntities();
            var query = (from rs in dc.report_skc_temp
                         join s in dc.Tm_Shop on rs.shopcode equals s.ShopCode
                         orderby rs.Year descending
                         select new
                         {
                             rs.Year,
                             rs.shopcode,
                             rs.shopname,
                             rs.Category,
                             rs.dictName,
                             rs.OperationAreaName,
                             rs.ASKC,
                             rs.BSKC,
                             rs.CSKC,
                             s.Season,
                         });
            if (!string.IsNullOrWhiteSpace(Keys))
                query = query.Where(t => t.shopcode.Contains(Keys) ||
                                         t.shopname.Contains(Keys));
            if (!string.IsNullOrWhiteSpace(Year))
                query = query.Where(t => t.Year.Contains(Year));
            if (!string.IsNullOrWhiteSpace(Season))
                query = query.Where(t => t.Season.Contains(Season));
            if (!string.IsNullOrWhiteSpace(Category))
                query = query.Where(t => t.Category == Category);
            var result = PaperData<ShelfDispalyModel.ReportSKCModel>.Create(query.Count(), Size, Page);
            result.Items = query.Skip(result.Skip)
                .Take(result.Limt).ToList()
                .Select(o => new ShelfDispalyModel.ReportSKCModel
                {
                    Year = o.Year,
                    Season = o.Season,
                    ShopCode = o.shopcode,
                    ShopName = o.shopname,
                    Category = o.Category,
                    OperationAreaName = o.OperationAreaName,
                    ParentAreaName = o.dictName,
                    ASKC = o.ASKC ?? 0,
                    BSKC = o.BSKC ?? 0,
                    CSKC = o.CSKC ?? 0,
                }).ToList();
            return Success(result);
        }
    }
}
